package cn.edu.sdu.sol.intake_system.service;

import cn.edu.sdu.sol.intake_system.controller.SuperAdminController;
import cn.edu.sdu.sol.intake_system.model.po.Gender;
import cn.edu.sdu.sol.intake_system.model.po.PersonInf;
import cn.edu.sdu.sol.intake_system.model.po.SysUser;
import cn.edu.sdu.sol.intake_system.model.vo.DataRequest;
import cn.edu.sdu.sol.intake_system.model.vo.DataResponse;
import cn.edu.sdu.sol.intake_system.repository.DepartmentRepository;
import cn.edu.sdu.sol.intake_system.repository.UserRepository;
import cn.edu.sdu.sol.intake_system.repository.WillRepository;
import cn.edu.sdu.sol.intake_system.utils.JwtUtil;
import cn.edu.sdu.sol.intake_system.utils.PersonInfUtils;
import cn.edu.sdu.sol.intake_system.utils.VO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class PersonService {

    @Autowired
    JwtUtil jwtUtil;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private SuperAdminController superAdminController;

    VO vo = new VO();

    @Autowired
    PersonInfUtils personInfUtils;

    @Autowired
    private WillService willService;

    public DataResponse update(DataRequest req, String token) {
        //System.out.println("into update!");
        token = jwtUtil.extractTokenFromHeader(token);
        String studentNum = jwtUtil.getStudentNumFromToken(token);
        Optional<SysUser> sysUser = userRepository.findUserByStudentNum(studentNum);
        if (sysUser.isEmpty()) {
            //System.out.println("error request!");
            return vo.messageError("can not find this user!");
        }
        //System.out.println("1.这里已经被执行了");
        SysUser user = sysUser.get();
        personInfUtils.fillPerson(user.getPersonInf(), req);


        userRepository.save(user);

        if (user.getPersonInf().getBirthday() == null) {
            return vo.messageOK("生日格式错误!");
        }
        return vo.ok();
    }


    private Map frontUtil1(String name, String label, Object value) {
        Map m = new HashMap();
        m.put("name", name);
        m.put("label", label);
        m.put("value", value);
        return m;
    }

    public DataResponse getPersonInf(String token) {
        //获取用户信息
        token = jwtUtil.extractTokenFromHeader(token);
        String studentNum = jwtUtil.getStudentNumFromToken(token);
        Optional<SysUser> sysUser = userRepository.findUserByStudentNum(studentNum);
        if (sysUser.isEmpty()) {
            return vo.messageError("用户不存在!");
        }
        SysUser s = sysUser.get();
        PersonInf p = s.getPersonInf();
        Integer personId = p.getPersonInfPk();

        Map m = new HashMap();

        List form = new ArrayList();

        Map m1 = frontUtil1("studentNum", "学号", studentNum);
        Map m2 = frontUtil1("headImg", "头像", s.getHeadImg());
        Map m3 = frontUtil1("personId", "人员ID", p.getPersonInfPk());
        Map m4 = frontUtil1("name", "姓名", p.getName());
        Map m12 = frontUtil1("introduction", "个人介绍", p.getIntroduction());

        form.add(m1);
        form.add(m2);
        form.add(m3);
        form.add(m4);
        form.add(m12);


        if (!superAdminController.isLocked) {
            String gender;
            if(p.getGender() == Gender.MALE){
                gender = "男";
            } else if(p.getGender() == Gender.FEMALE){
                gender = "女";
            } else {
                gender = "未知";
            }
            Map m5 = frontUtil1("gender", "性别", gender);
            Map m6 = frontUtil1("academy", "学院", p.getAcademy());
            Map m7 = frontUtil1("qqNum", "qq", p.getQqNum());
            Map m8 = frontUtil1("phoneNum", "手机号", p.getPhoneNum());
            Map m9 = frontUtil1("profession", "专业", p.getProfession());
            Map m10 = frontUtil1("birthday", "出生日期", p.getBirthday());
            Map m11 = frontUtil1("email", "邮箱", p.getEmail());
            Map m13 = frontUtil1("school", "校区", p.getSchool());
            Map m14 = frontUtil1("birthPlace", "籍贯", p.getBirthPlace());

            form.add(m5);
            form.add(m6);
            form.add(m7);
            form.add(m8);
            form.add(m9);
            form.add(m10);
            form.add(m11);
            form.add(m13);
            form.add(m14);
        }


        List wills = willService.getWills(personId);
        Map willList = frontUtil1("willList", "志愿", wills);

        m.put("form", form);
        m.put("willList", willList);

        return new DataResponse(200, m, "ok");
    }
}
